﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SBACN.WebSite.Code;
using System.Linq.Expressions;
using SBACN.WebSite.Models;

namespace SBACN.WebSite.Admin
{
	public partial class ActivityList : AdminBasePage
	{
		/// <summary>
		/// 查看新闻的系统字典类型
		/// </summary>
		public string SystemType
		{
			get
			{
				//if (ViewState["SystemType"] == null)
				//    return "00020004";//要闻导读，默认值
				//else
				//    return ViewState["SystemType"].ToString();
				return this.SystemCode;
			}
			private set
			{
				ViewState["SystemType"] = this.SystemCode;
			}
		}

		/// <summary>
		/// 是否是扩展编辑
		/// </summary>
		public bool ExEdit
		{
			get
			{
				if (ViewState["EditType"] == null)
				{
					return false;
				}
				else
				{
					return Convert.ToBoolean(ViewState["EditType"]);
				}
			}
			private set
			{
				ViewState["EditType"] = value;
			}
		}

		/// <summary>
		/// 是否发布到英文站点
		/// </summary>
		public bool IsEN
		{
			get
			{
				if (ViewState["IsEN"] == null)
				{
					return false;
				}
				else
				{
					return Convert.ToBoolean(ViewState["IsEN"]);
				}
			}
			private set
			{
				ViewState["IsEN"] = value;
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				if (!string.IsNullOrWhiteSpace(Request["st"]))
				{
					this.SystemType = Request["st"].Trim();
				}

				if (!string.IsNullOrWhiteSpace(Request["ex"]))
				{
					this.ExEdit = Request["ex"].Trim() == "1";
				}
				else
				{
					this.ExEdit = false;
				}

				if (!string.IsNullOrWhiteSpace(Request["en"]))
				{
					this.IsEN = Request["en"].Trim() == "1";
				}
				else
				{
					this.IsEN = false;
				}
				BindData(1);
			}
		}

		private void BindData(int pageIndex)
		{
			Expression<Func<TB_Activities, bool>> f = (p => true);
			Expression<Func<TB_Activities, DateTime>> o = (p => p.CreateDate);
			Expression<Func<TB_Activities, int>> then = (p => p.ID);
			int records = 0;
			var newsList = DAL.DALHelper.Instance.GetList<TB_Activities, DateTime, int>(f, o, false, then, false, this.Pager1.PageSize, pageIndex, out records);
			this.rpt_News.DataSource = newsList;
			this.rpt_News.DataBind();
			this.Pager1.RecordCount = records;
			this.Pager1.InitControl();
		}

		public void BindSelectedPageIndexChanged(int pageIndex, int nextPage)
		{
			BindData(nextPage);
		}

		protected void btn_Delete_Click(object sender, EventArgs e)
		{
			int newsID = 0;
			if (!string.IsNullOrWhiteSpace(this.hid_ID.Value) && int.TryParse(this.hid_ID.Value, out newsID))
			{
				//  检查活动下是否有报道并提示。
				Expression<Func<TB_MediaReports, bool>> f = (p => p.ActivityID == newsID);
				var result_MediaReportsList = DAL.DALHelper.Instance.GetList<TB_MediaReports>(f);
				if (result_MediaReportsList.Count > 0)
				{
					ClientScript.RegisterClientScriptBlock(this.GetType(),"AlertMsg","<script type='text/javascript'>alert('删除失败！删除活动前请先删除此活动下的所有报道。')</script>");
					ClientScript.RegisterClientScriptBlock(this.GetType(), "Msg", "$jqy(document).ready(function () {RefreshNewsList();});", true);
					return;
				}
				else
				{
					DAL.DALHelper.Instance.DeleteWithTransaction<TB_Activities>("ID", newsID);
					BindData(1);
				}

			}
			this.hid_ID.Value = string.Empty;
		}

		protected void btn_NewsRefresh_Click(object sender, EventArgs e)
		{
			BindData(this.Pager1.CurrentPageIndex);
		}
	}
}